/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package graphviewer;

import service.Service;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Sabitov Artem
 * @since 23.09.2013
 */
public class GraphMove extends JPanel implements MouseMotionListener, MouseListener {
	public final int infelicity = 20;
	
	private int oldX;
	private int oldY;
	private Graph graph;
	
	private int maxX;
	private int maxY;
	private int minX;
	private int minY;
	
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
		setBounds(minX, minY, maxX - minX + 80, maxY - minY + 30);
		g2d.drawRect(1, 1, maxX - minX - 3, maxY - minY - 3);
		Font font = new Font("Arial", 0, 9);
		g2d.setFont(font);
		g2d.drawString("id: " + String.valueOf(graph.getID()) + " | node count:" + graph.getSizeGraph(), 2, maxY - minY + 8);
		g2d.drawString("min x: " + minX + " | max x: " + maxX, 2, maxY - minY + 18);
		g2d.drawString("min y: " + minY + " | max y: " + maxY, 2, maxY - minY + 28);
	}
	
	private void searchBounds() {
		minX = 9999;
		minY = 9999;
		maxX = 0;
		maxY = 0;
		
		for(int i = 0; i < graph.getSizeGraph(); i++) {
			Node node = graph.getNode(i);
			int x = (int) node.getCoordX();
			int y = (int) node.getCoordY();
			if(x + infelicity > maxX) {
				maxX = (int) x + infelicity;
			}
			if(x - infelicity < minX) {
				minX = (int) x - infelicity;
			}
			if(y + infelicity > maxY) {
				maxY = (int) y + infelicity;
			}
			if(y - infelicity < minY) {
				minY = (int) y - infelicity;
			}
		}
		repaint();
	}
	
	public GraphMove(JFrame frame, Graph graph) {
		this.graph = graph;
		searchBounds();
		if(Service.getServiceState()) {
			initMove(frame);
		}
		initMouseListener(frame);
	}
	
	private void initMove(JFrame frame) {
		setBackground(Color.RED);
		setOpaque(false);
		frame.add(this);
		setBounds(minX, minY, maxX - minX + 80, maxY - minY + 30);
	}
	
	private void initMouseListener(JFrame objFrame) {
		objFrame.addMouseMotionListener(this);
		objFrame.addMouseListener(this);
	}
	
	private boolean inGraph(int x, int y) {
		if (minX+8 < x && x < maxX+8
				&& minY+30 < y && y < maxY+30) {
			return true;
		} else return false;
	}
	
	@Override
	public void mouseDragged(MouseEvent me) {
//		searchBounds();
		if(inGraph(me.getX(), me.getY())) {
//			for(int i = 0; i < graph.getSizeGraph(); i++) {
//				Node node = graph.getNode(i);
//				node.setCoords(node.getCoordX() + (me.getX() - node.getCoordX()),
//						node.getCoordY() + (me.getY()) - node.getCoordY());
//			}
			double offsetX = (double) me.getX() / (double) oldX;
			double offsetY = (double) me.getY() / (double) oldY;

			oldX = me.getX();
			oldY = me.getY();
			
			if(offsetX > 1) {
				for(int i = 0; i < graph.getSizeGraph(); i++) {
					Node node = graph.getNode(i);
					node.setCoords(node.getCoordX() + 1.5, node.getCoordY());
				}
			} else if(offsetX < 1) {
				for(int i = 0; i < graph.getSizeGraph(); i++) {
					Node node = graph.getNode(i);
					node.setCoords(node.getCoordX() - 1.5, node.getCoordY());
				}
			}
			if(offsetY > 1) {
				for(int i = 0; i < graph.getSizeGraph(); i++) {
					Node node = graph.getNode(i);
					node.setCoords(node.getCoordX(), node.getCoordY() + 1.5);
				}
			} else if(offsetY < 1) {
				for(int i = 0; i < graph.getSizeGraph(); i++) {
					Node node = graph.getNode(i);
					node.setCoords(node.getCoordX(), node.getCoordY() - 1.5);
				}
			}
		}
		update();
	}

	@Override
	public void mouseMoved(MouseEvent me) {
	}

	@Override
	public void mouseClicked(MouseEvent me) {
	}

	@Override
	public void mousePressed(MouseEvent me) {
	}

	@Override
	public void mouseReleased(MouseEvent me) {
	}

	@Override
	public void mouseEntered(MouseEvent me) {
	}

	@Override
	public void mouseExited(MouseEvent me) {
	}
	
	public void update() {
		searchBounds();
	}
}
